home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / upc12bs1.zip / MAIL / sysalias.c < prev    next >
C/C++ Source or Header  |  1993-04-10  |  9KB  |  230 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    S y s A l i a s . C                                             */
  3. /*                                                                    */
  4. /*    System wide alias support for UUPC/extended                     */
  5. /*--------------------------------------------------------------------*/
  6.  
  7. /*--------------------------------------------------------------------*/
  8. /*    Changes Copyright (c) 1990-1993 by Kendra Electronic            */
  9. /*    Wonderworks.                                                    */
  10. /*                                                                    */
  11. /*    All rights reserved except those explicitly granted by the      */
  12. /*    UUPC/extended license agreement.                                */
  13. /*--------------------------------------------------------------------*/
  14.  
  15. /*--------------------------------------------------------------------*/
  16. /*                          RCS Information                           */
  17. /*--------------------------------------------------------------------*/
  18.  
  19. /*
  20.  *    $Id: SYSALIAS.C 1.3 1993/04/11 00:33:05 ahd Exp $
  21.  *
  22.  *    $Log: SYSALIAS.C $
  23.  * Revision 1.3  1993/04/11  00:33:05  ahd
  24.  * Global edits for year, TEXT, etc.
  25.  *
  26.  * Revision 1.2  1992/12/05  23:38:43  ahd
  27.  * Skip blanks as well as unprintable characters
  28.  *
  29.  * Revision 1.1  1992/12/04  01:00:27  ahd
  30.  * Initial revision
  31.  *
  32.  */
  33.  
  34. /*--------------------------------------------------------------------*/
  35. /*                        System include files                        */
  36. /*--------------------------------------------------------------------*/
  37.  
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include <time.h>
  42. #include <limits.h>
  43. #include <ctype.h>
  44.  
  45. /*--------------------------------------------------------------------*/
  46. /*                    UUPC/extended include files                     */
  47. /*--------------------------------------------------------------------*/
  48.  
  49. #include "lib.h"
  50. #include "deliver.h"
  51. #include "sysalias.h"
  52. #include "hlib.h"
  53.  
  54. /*--------------------------------------------------------------------*/
  55. /*                          Global variables                          */
  56. /*--------------------------------------------------------------------*/
  57.  
  58. char *SysAliases = NULL;      /* Name of our system alias file       */
  59. static ALIASTABLE *aliasTable = NULL;
  60. static int aliases = 0;
  61.  
  62. currentfile();
  63.  
  64. /*--------------------------------------------------------------------*/
  65. /*                             Prototypes                             */
  66. /*--------------------------------------------------------------------*/
  67.  
  68. static void InitAlias( void );
  69.  
  70. /*--------------------------------------------------------------------*/
  71. /*    c h e c k a l i a s                                             */
  72. /*                                                                    */
  73. /*    Check the system alias table for a user.  We perform a linear   */
  74. /*    search on the unordered table we we don't expect to perform     */
  75. /*    many searchs in one execution of the program, and this saves    */
  76. /*    sorting it.                                                     */
  77. /*--------------------------------------------------------------------*/
  78.  
  79. ALIASTABLE *checkalias( const char *user )
  80. {
  81.    int subscript = 0;
  82.  
  83.    if ( SysAliases == NULL )
  84.       InitAlias();
  85.  
  86.    for ( subscript = 0; subscript < aliases; subscript ++ )
  87.    {
  88.       if ( equali(aliasTable[subscript].alias , user ))
  89.          return &aliasTable[subscript];
  90.    } /* for */
  91.  
  92. /*--------------------------------------------------------------------*/
  93. /*                        No hit, return NULL                         */
  94. /*--------------------------------------------------------------------*/
  95.  
  96.    return NULL;
  97. } /* checkalias */
  98.  
  99. /*--------------------------------------------------------------------*/
  100. /*       I n i t A l i a s                                            */
  101. /*                                                                    */
  102. /*       Initialize our system alias table                            */
  103. /*--------------------------------------------------------------------*/
  104.  
  105. static void InitAlias( void )
  106. {
  107.    char buf[BUFSIZ];
  108.    int subscript  = -1;
  109.    int maxaliases = 64;
  110.    boolean inAlias = FALSE;
  111.    FILE *stream;
  112.    long here;
  113.  
  114. /*--------------------------------------------------------------------*/
  115. /*            Build the file name and try to open the file            */
  116. /*--------------------------------------------------------------------*/
  117.  
  118.    mkfilename( buf, E_confdir, "aliases" );
  119.    SysAliases = newstr( buf );
  120.  
  121.    stream = FOPEN( SysAliases , "r",TEXT_MODE );
  122.    if ( stream == NULL )
  123.    {
  124.       if (debuglevel > 1)
  125.          printerr( SysAliases );
  126.       return;
  127.    } /* if */
  128.  
  129. /*--------------------------------------------------------------------*/
  130. /*                 The file is open, allocate a table                 */
  131. /*--------------------------------------------------------------------*/
  132.  
  133.    aliasTable = malloc( sizeof *aliasTable * maxaliases );
  134.  
  135.    here = ftell( stream );       /* Remember location in file        */
  136.  
  137. /*--------------------------------------------------------------------*/
  138. /*                    Begin loop to process names                     */
  139. /*--------------------------------------------------------------------*/
  140.  
  141.    while (fgets( buf , BUFSIZ , stream ) != NULL )
  142.    {
  143.       char *s = buf;
  144.  
  145. /*--------------------------------------------------------------------*/
  146. /*    Ignore comments, lines that begin with whitespace, and empty    */
  147. /*    lines                                                           */
  148. /*--------------------------------------------------------------------*/
  149.  
  150.       while( *s && ! isgraph( *s ))
  151.          s++;
  152.  
  153.       if (*s == '#')
  154.          continue;
  155.  
  156.       if ( ! *s )                /* Empty line?                      */
  157.       {                          /* Yes --> 'tis end of alias        */
  158.          if ( inAlias )
  159.          {
  160.             inAlias = FALSE;
  161.             if (aliasTable[subscript].start == -1 )
  162.                printmsg(0,"%s: Invalid alias %s, no data defined!",
  163.                   SysAliases, aliasTable[subscript].alias );
  164.             else
  165.                aliasTable[subscript].end = here;
  166.          }
  167.       } /* if ( ! *s ) */
  168.       else if (inAlias)
  169.       {
  170.          if (aliasTable[subscript].start == -1 )
  171.             aliasTable[subscript].start = here + (s - buf);
  172.       }
  173.       else {                           /* Start of a new alias */
  174.          char *colon = strchr( s, ':' );
  175.  
  176.          if (s != buf )
  177.             printmsg(0,"%s: Error aliases must begin in column 1",
  178.                   SysAliases );
  179.  
  180.          if (colon == NULL )
  181.          {
  182.             printmsg(0,"%s: No colon after alias %s",
  183.                   SysAliases, s);
  184.             continue;
  185.          }
  186.  
  187.          if ( subscript+2 == maxaliases )
  188.          {
  189.             maxaliases *= 2;
  190.             aliasTable = realloc( aliasTable,
  191.                                   maxaliases * sizeof *aliasTable );
  192.             checkref( aliasTable );
  193.          }
  194.  
  195.          *colon = '\0';          /* Terminate the name               */
  196.          aliasTable[++subscript].alias = newstr(strtok(s,WHITESPACE));
  197.                                  /* Save name of alias               */
  198.          s = strtok( colon+1, WHITESPACE);
  199.                                  /* Find next token                  */
  200.  
  201.          if ((s == NULL) || (*s == '#'))  /* Any alias on same?      */
  202.              aliasTable[subscript].start = -1;
  203.                                  /* No--> Look for it later          */
  204.          else
  205.              aliasTable[subscript].start = here + ( s - buf );
  206.  
  207.           aliasTable[subscript].end  = -1;
  208.           inAlias = TRUE;
  209.       } /* if */
  210.  
  211.       here = ftell( stream );       /* Remember start of next line   */
  212.  
  213.    } /* while */
  214.  
  215. /*--------------------------------------------------------------------*/
  216. /*                   Clean up and return to caller                    */
  217. /*--------------------------------------------------------------------*/
  218.  
  219.    if ( inAlias )
  220.       aliasTable[subscript].end = LONG_MAX;
  221.  
  222.    aliases = subscript + 1;
  223.    if ( aliases == 0 )
  224.       free( aliasTable );
  225.    else
  226.       aliasTable = realloc( aliasTable, aliases * sizeof *aliasTable );
  227.    fclose( stream );
  228.  
  229. } /* InitAlias */
  230.